package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.*;
import com.heima.model.behavior.vos.ArticleBehaviorVo;
import com.heima.model.common.constants.user.UserRelationConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class ApArticleBehaviorImpl implements ApArticleBehaviorService {

    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    StringRedisTemplate redisTemplate;

    /**
     * 文章关系展示
     * 是否已经关注该文章的作者、是否收藏了此文章、是否点赞了文章、是否不喜欢该文章等
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {
        /**
         * 1. 判断用户是否登录
         * 2. 未登录 直接将 4种行为 均返回false
         * 3. 如果登录
         * 4. 查询行为实体
         * 5. 根据行为实体 文章ID查询 是否点赞
         * 6. 根据行为实体 文章ID查询 是否不喜欢
         * 7. 根据行为实体 文章ID查询 是否收藏
         * 8. 更具登录用户id 去redis中查询是否关注该作者
         * 9. 封装结果 返回
         */
        ApUser user = AppThreadLocalUtils.getUser();
        //未登录直接返回
        if (user == null) {
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
            ArticleBehaviorVo vo = new ArticleBehaviorVo();
            vo.setIscollection(false);
            vo.setIsfollow(false);
            vo.setIslike(false);
            vo.setIsunlike(false);
            return ResponseResult.okResult(vo);
        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        if (apBehaviorEntry==null) {
            ArticleBehaviorVo vo = new ArticleBehaviorVo();
            vo.setIscollection(false);
            vo.setIsfollow(false);
            vo.setIslike(false);
            vo.setIsunlike(false);
            return ResponseResult.okResult(vo);
        }
        ArticleBehaviorVo behaviorVo = new ArticleBehaviorVo();
        //查询实体行为
        Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
        ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
        ApLikesBehavior likesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
        ApUnlikesBehavior unlikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
        //Redis查询关注行为
        Integer loginId = user.getId();
        //获取作者用户id
        Integer followId = dto.getAuthorApUserId();
        Double score = redisTemplate.opsForZSet()
                .score(UserRelationConstants.FOLLOW_LIST + loginId, String.valueOf(followId));

        //判断是否存在行为
        if (score != null) {
            behaviorVo.setIsfollow(true);
        } else {
            behaviorVo.setIsfollow(false);
        }

        if (likesBehavior.getOperation()==0) {
            behaviorVo.setIslike(true);
        } else {
            behaviorVo.setIslike(false);
        }
        if (unlikesBehavior.getType() == 0) {
            behaviorVo.setIsunlike(true);
        } else {
            behaviorVo.setIsunlike(false);
        }
        if (apCollection.getType() == 0) {
            behaviorVo.setIscollection(true);
        } else {
            behaviorVo.setIscollection(false);
        }
        return ResponseResult.okResult(behaviorVo);
    }
}
